package com.ylw.uibasic;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private Button firstButton;
	
	
	private static final int MESSAGE_REQUEST_ID = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		firstButton = (Button)this.findViewById(R.id.first_button);
		
		firstButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Invoke another Activity
				Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
				startActivityForResult(intent, MESSAGE_REQUEST_ID);
			}
			
		});
		
		//Tutorial 8: Web View
		Button webViewButton = (Button)this.findViewById(R.id.webViewButton);
		
		webViewButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// Open Web View Activity
				Intent webActivity = new Intent(MainActivity.this, WebActivity.class);
				startActivity(webActivity);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Get back from other activity
		String msg = null;
		if(requestCode == MESSAGE_REQUEST_ID) {
			if(data != null) {
				msg = data.getExtras().getString("com.ylw.uibasic.message");
			}
			else {
				msg = "No worthy message returned";
			}
		}
		else {
			msg = "Not the right request code " + requestCode;
		}
		
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
